home *** CD-ROM | disk | FTP | other *** search
/ Aminet 40 / Aminet 40 (2000)(Schatztruhe)[!][Dec 2000].iso / Aminet / dev / lang / Python16_Src.lha / Python16_Source / Objects / cobject.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-09-10  |  3.4 KB  |  161 lines

  1. /* Wrap void* pointers to be passed between C modules */
  2.  
  3. #include "Python.h"
  4.  
  5. /* Declarations for objects of type PyCObject */
  6.  
  7. typedef void (*destructor1) Py_PROTO((void *));
  8. typedef void (*destructor2) Py_PROTO((void *, void*));
  9.  
  10. typedef struct {
  11.     PyObject_HEAD
  12.     void *cobject;
  13.         void *desc;
  14.     void (*destructor) Py_PROTO((void *));
  15. } PyCObject;
  16.  
  17. #include "protos/cobject.h"
  18.  
  19. PyObject *
  20. PyCObject_FromVoidPtr(cobj, destr)
  21.     void *cobj;
  22.     void (*destr) Py_PROTO((void *));
  23. {
  24.     PyCObject *self;
  25.     
  26.     self = PyObject_NEW(PyCObject, &PyCObject_Type);
  27.     if (self == NULL)
  28.         return NULL;
  29.     self->cobject=cobj;
  30.     self->destructor=destr;
  31.     self->desc=NULL;
  32.     return (PyObject *)self;
  33. }
  34.  
  35. PyObject *
  36. PyCObject_FromVoidPtrAndDesc(cobj, desc, destr)
  37.     void *cobj;
  38.     void *desc;
  39.     void (*destr) Py_PROTO((void *, void *));
  40. {
  41.     PyCObject *self;
  42.  
  43.     if(!desc) {
  44.             PyErr_SetString(PyExc_TypeError,
  45.           "PyCObject_FromVoidPtrAndDesc called with null description");
  46.         return NULL;
  47.     }
  48.     
  49.     self = PyObject_NEW(PyCObject, &PyCObject_Type);
  50.     if (self == NULL)
  51.         return NULL;
  52.     self->cobject=cobj;
  53.     self->destructor=(destructor1)destr;
  54.     self->desc=desc;
  55.     return (PyObject *)self;
  56. }
  57.  
  58. void *
  59. PyCObject_AsVoidPtr(self)
  60.     PyObject *self;
  61. {
  62.         if(self)
  63.       {
  64.         if(self->ob_type == &PyCObject_Type)
  65.           return ((PyCObject *)self)->cobject;
  66.         PyErr_SetString(PyExc_TypeError,
  67.                 "PyCObject_AsVoidPtr with non-C-object");
  68.       }
  69.     if(! PyErr_Occurred())
  70.         PyErr_SetString(PyExc_TypeError,
  71.                 "PyCObject_AsVoidPtr called with null pointer");
  72.     return NULL;
  73. }
  74.  
  75. void *
  76. PyCObject_GetDesc(self)
  77.     PyObject *self;
  78. {
  79.         if(self)
  80.       {
  81.         if(self->ob_type == &PyCObject_Type)
  82.           return ((PyCObject *)self)->desc;
  83.         PyErr_SetString(PyExc_TypeError,
  84.                 "PyCObject_GetDesc with non-C-object");
  85.       }
  86.     if(! PyErr_Occurred())
  87.         PyErr_SetString(PyExc_TypeError,
  88.                 "PyCObject_GetDesc called with null pointer");
  89.     return NULL;
  90. }
  91.  
  92. void *
  93. PyCObject_Import(module_name, name)
  94.      char *module_name;
  95.      char *name;
  96. {
  97.   PyObject *m, *c;
  98.   void *r=NULL;
  99.   
  100.   if((m=PyImport_ImportModule(module_name)))
  101.     {
  102.       if((c=PyObject_GetAttrString(m,name)))
  103.     {
  104.       r=PyCObject_AsVoidPtr(c);
  105.       Py_DECREF(c);
  106.     }
  107.       Py_DECREF(m);
  108.     }
  109.  
  110.   return r;
  111. }
  112.  
  113. static void
  114. PyCObject_dealloc(self)
  115.     PyCObject *self;
  116. {
  117.         if(self->destructor)
  118.       {
  119.         if(self->desc)
  120.               ((destructor2)(self->destructor))(self->cobject, self->desc);
  121.         else
  122.               (self->destructor)(self->cobject);
  123.       }
  124.     PyObject_DEL(self);
  125. }
  126.  
  127.  
  128. static char PyCObject_Type__doc__[] = 
  129. "C objects to be exported from one extension module to another\n\
  130. \n\
  131. C objects are used for communication between extension modules.  They\n\
  132. provide a way for an extension module to export a C interface to other\n\
  133. extension modules, so that extension modules can use the Python import\n\
  134. mechanism to link to one another.\n"
  135. ;
  136.  
  137. PyTypeObject PyCObject_Type = {
  138.     PyObject_HEAD_INIT(&PyType_Type)
  139.     0,                /*ob_size*/
  140.     "PyCObject",            /*tp_name*/
  141.     sizeof(PyCObject),        /*tp_basicsize*/
  142.     0,                /*tp_itemsize*/
  143.     /* methods */
  144.     (destructor)PyCObject_dealloc,    /*tp_dealloc*/
  145.     (printfunc)0,        /*tp_print*/
  146.     (getattrfunc)0,    /*tp_getattr*/
  147.     (setattrfunc)0,    /*tp_setattr*/
  148.     (cmpfunc)0,        /*tp_compare*/
  149.     (reprfunc)0,        /*tp_repr*/
  150.     0,            /*tp_as_number*/
  151.     0,        /*tp_as_sequence*/
  152.     0,        /*tp_as_mapping*/
  153.     (hashfunc)0,        /*tp_hash*/
  154.     (ternaryfunc)0,        /*tp_call*/
  155.     (reprfunc)0,        /*tp_str*/
  156.  
  157.     /* Space for future expansion */
  158.     0L,0L,0L,0L,
  159.     PyCObject_Type__doc__ /* Documentation string */
  160. };
  161.